package com.haierCamera.customapplication.ui.user;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.databinding.HzklActivityWebViewPublicBinding;
import com.haierCamera.customapplication.di.TokenManager;

import javax.inject.Inject;

/**
 * Created by AdministratorZJ
 * on 2019/10/18.
 */
public class WebNoticePublicActivity extends BaseActivity {
    private HzklActivityWebViewPublicBinding webViewBinding;
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private String id;
    @Inject
    TokenManager tokenManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        webViewBinding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_web_view_public);


        initData();
//        initDataNew();
    }

    private void initDataNew() {
        String title = getIntent().getStringExtra("title");
        String url = getIntent().getStringExtra("url");

        Log.i("TAG", "url-" + url);
        webViewBinding.toolbar.setTitle(title);
        setSupportActionBar(webViewBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        webViewBinding.editor.setPlaceholder(url);
        webViewBinding.editor.setPadding(20, 20, 20, 20);
        webViewBinding.editor.setTextBackgroundColor(Color.WHITE);
        webViewBinding.editor.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    webViewBinding.progressBar.setVisibility(View.GONE);
                } else {
                    webViewBinding.progressBar.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void initData() {
        //getPermission();
        String title = getIntent().getStringExtra("title");
        String time = getIntent().getStringExtra("time");
        String url = getIntent().getStringExtra("url");
        webViewBinding.title.setText(title);
        webViewBinding.time.setText(time);
        Log.i("TAG", "url-" + url);
        webViewBinding.toolbar.setTitle(title);
        setSupportActionBar(webViewBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//        WebSettings settings = webViewBinding.webView.getSettings();
//        settings.setAllowFileAccess(true);
//        settings.setAllowFileAccessFromFileURLs(false);
//        settings.setAllowUniversalAccessFromFileURLs(false);
//        // 禁止 file 协议加载 JavaScript
//        if (url.startsWith("file://")) {
//            settings.setJavaScriptEnabled(false);
//        } else {
//            settings.setJavaScriptEnabled(true);
//        }
//        settings.setAllowFileAccess(true);
//        settings.setDomStorageEnabled(true);//这设置存储 不设置显示不出来
//        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//没有网使用缓存
//        settings.setUseWideViewPort(true);//设置视图可缩放和下面一起设置 似乎没效果
//        settings.setLoadWithOverviewMode(true);
//
//
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
//            settings.setAllowFileAccessFromFileURLs(true);
//            settings.setAllowUniversalAccessFromFileURLs(true);
//        }
//        webViewBinding.webView.setWebViewClient(new WebViewClient() {
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//                view.loadUrl(request.getUrl().toString());
//                return super.shouldOverrideUrlLoading(view, request);
//            }
//        });
//        webViewBinding.webView.setWebChromeClient(new WebChromeClient() {
//            @Override
//            public void onProgressChanged(WebView view, int newProgress) {
//                super.onProgressChanged(view, newProgress);
//                if (newProgress == 100) {
//                    webViewBinding.progressBar.setVisibility(View.GONE);
//                } else {
//                    webViewBinding.progressBar.setVisibility(View.VISIBLE);
//                }
//            }
//        });
////        webViewBinding.webView.loadUrl(url);
        webViewBinding.webView.loadDataWithBaseURL("", url
                , "text/html", "utf-8", null);
        WebSettings set = webViewBinding.webView.getSettings();
        set.setTextZoom(100);
    }

    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };


    private void getPermission() {
        int hasWriteContactsPermission = ContextCompat.checkSelfPermission(WebNoticePublicActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE);
        if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
            if (!ActivityCompat.shouldShowRequestPermissionRationale(WebNoticePublicActivity.this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
                ActivityCompat.requestPermissions(WebNoticePublicActivity.this,
                        PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
            }

            ActivityCompat.requestPermissions(WebNoticePublicActivity.this,
                    PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
        }

        while ((ContextCompat.checkSelfPermission(WebNoticePublicActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE)) != PackageManager.PERMISSION_GRANTED) {
        }
    }

}
